﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using DG.Tweening;
using UnityEngine.UI;

public class FishView : MonoBehaviour {

    private GameObject fishGo;
    private GameObject enemyGo;
    private FishAttack fa;

    public FishData fdata;
    public int fishPos;
    public Slider healthBar;
    public Text DamageText;
    public Text manaText;
    public Image Weapon;
    public int hurtDamage;
    public Text SpeedText;
    public Image atkbuf;
    public Image spbuf;

    // Use this for initialization
    void Awake () {
        fishGo = this.gameObject;
        manaText.text = 0.ToString();
    }

    void Start()
    {
        fdata.OnManaChanged += OnManaChanged;
        fdata.OnHPChanged += OnHPChanged;
        SetColor(fdata.WeaponAttri);
        fa = GetComponent<FishAttack>();
        SpeedText.text = fdata.Sp.ToString();
    }

    //角色被摧毁时
    private void OnDestroy()
    {
        fdata.OnManaChanged -= OnManaChanged;
        fdata.OnHPChanged -= OnHPChanged;
    }
    
    //攻击表现
    public void Attack(int damage, GameObject enGo)
    {
        enemyGo = enGo;
        FishView enemyFv = enemyGo.GetComponent<FishView>();

        //生成子弹
        GameObject bulletGo = Resources.Load("Bullet") as GameObject;
        GameObject bullet = Instantiate(bulletGo, fishGo.transform);

        //改变子弹和攻击者的颜色
        bullet.GetComponent<Renderer>().material.color = Color.red;
        GetComponent<Renderer>().material.color = Color.green;

        //子弹飞行
        bullet.transform.DOMove(enGo.transform.position, 1)
            .SetEase(Ease.Flash).OnComplete((() => enemyFv.FishHurt(damage, fa.attackType, bullet)));

        //回合结束
        StartCoroutine(ColorBack());
        
    }

    //攻击表现多态，多人受伤的时候
    public void Attack(ArrayList damageList, List<GameObject> enGoList)
    {
        for(int i = 0; i< enGoList.Count; i++)
        {
            FishView enemyFv = enGoList[i].GetComponent<FishView>();

            //生成子弹
            GameObject bulletGo = Resources.Load("Bullet") as GameObject;
            GameObject bullet = Instantiate(bulletGo, fishGo.transform);

            //改变子弹和攻击者的颜色
            bullet.GetComponent<Renderer>().material.color = Color.red;
            GetComponent<Renderer>().material.color = Color.green;

            enemyFv.hurtDamage = (int)damageList[i];

            //子弹飞行
            bullet.transform.DOMove(enGoList[i].transform.position, 1)
                .OnComplete((() => enemyFv.FishHurt(enemyFv.hurtDamage, fa.attackType, bullet)));
        }
        //回合结束
        StartCoroutine(ColorBack());
    }

    //受伤
    void FishHurt(int damage, AttackType atk, GameObject bullet)
    {
        GetComponent<Renderer>().material.color = Color.red;

        //显示伤害数字
        SetDamageText(damage, atk);

        //改变血条
        float v = (float)fdata.Hp / (float)fdata.MaxHp;
        healthBar.value = v;

        Destroy(bullet);

        OnDie();
    }

    //结束回合
    IEnumerator EndRound()
    {
        yield return new WaitForSeconds(0.5f);

        GameControl._instance.mState = OperatorState.Round;
        GameControl._instance.ButtleTurn();
    }

    //恢复伤害字体的位置
    IEnumerator CloseDamage()
    {
        yield return new WaitForSeconds(1f);
        
        DamageText.gameObject.SetActive(false);

        DamageText.transform.localPosition = Vector3.zero;
    }

    //销毁子弹，攻击和受击者的颜色
    IEnumerator ColorBack()
    {
        yield return new WaitForSeconds(1.5f);
        if (enemyGo != null)
        {
            enemyGo.GetComponent<Renderer>().material.color = Color.white;
        }
        GetComponent<Renderer>().material.color = Color.white;

        StartCoroutine(EndRound());
    }

    //更新魔法显示
    void UpdateShow()
    {
        manaText.text = fdata.Mana.ToString();
        SpeedText.text = fdata.Sp.ToString();

        if (fdata.SkillBuff.Count == 0)
        {

            atkbuf.gameObject.SetActive(false);

            spbuf.gameObject.SetActive(false);

            return;
        }

        foreach (BuffData bd in fdata.SkillBuff)
        {
            if (bd.Icon == "atk")
            {
                atkbuf.gameObject.SetActive(true);
            }
            else if (bd.Icon == "sp")
            {
                spbuf.gameObject.SetActive(true);
            }
        }

    }

    //响应魔法变化的事件
    void OnManaChanged()
    {
        UpdateShow();
    }

    void OnHPChanged(int damage)
    {
        //改变血条
        float v = (float)fdata.Hp / (float)fdata.MaxHp;
        healthBar.value = v;

        SetDamageText(damage ,AttackType.Health);
    }

    //死亡判断处理
    void OnDie()
    {
        if (fdata.Hp <= 0)
        {
            GameControl._instance.DeleteHero(gameObject);
        }
        
    }

    //显示武器颜色
    void SetColor(int i)
    {
        switch (i)
        {
            case 0:
                Weapon.overrideSprite = Resources.Load("Texture/red", typeof(Sprite)) as Sprite;
                break;
            case 1:
                Weapon.overrideSprite = Resources.Load("Texture/green", typeof(Sprite)) as Sprite;
                break;
            case 2:
                Weapon.overrideSprite = Resources.Load("Texture/blue", typeof(Sprite)) as Sprite;
                break;
            case 3:
                Weapon.overrideSprite = Resources.Load("Texture/grey", typeof(Sprite)) as Sprite;
                break;

        }
    }

    //改变字体颜色
    void SetDamageText(int damage, AttackType atk)
    {
        DamageText.gameObject.SetActive(true);

        switch (atk)
        {
            case AttackType.Normal:
                DamageText.color = Color.red;
                DamageText.text = damage.ToString();
                DamageText.transform.DOLocalMove(Vector2.up, 1f);
                break;
            case AttackType.Crit:
                DamageText.color = Color.yellow;
                DamageText.text = damage.ToString();
                DamageText.transform.DOLocalMove(Vector2.up, 1f);
                break;
            case AttackType.Miss:
                DamageText.color = Color.white;
                DamageText.text = "Miss";
                DamageText.transform.DOLocalMove(Vector2.up, 1f);
                break;
            case AttackType.Skill:
                DamageText.color = Color.cyan;
                DamageText.text = damage.ToString();
                DamageText.transform.DOLocalMove(Vector2.up, 1f);
                break;
            case AttackType.Health:
                DamageText.color = Color.green;
                DamageText.text = damage.ToString();
                DamageText.transform.DOLocalMove(Vector2.up, 1f);
                break;

        }
        StartCoroutine(CloseDamage());
    }
}
